﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using PMSMVC.Models;
using Webdiyer.WebControls.Mvc;

namespace PMSMVC.Controllers
{
    [Authorize]
    public class ProjectController : Controller
    {
        private PMSDBContext db = new PMSDBContext();

        //
        // GET: /Project/

        public ViewResult Index(int? id)
        {
            PagedList<Project> list = db.Projects.OrderBy(p => p.ProductTypes.FirstOrDefault().CustomerID).ToPagedList(id ?? 1, 10);
            return View(list);
        }

        //
        // GET: /Project/Details/5

        public ViewResult Details(string id)
        {
            Project project = db.Projects.Find(id);
            return View(project);
        }

        //
        // GET: /Project/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Project/Create

        [HttpPost]
        public ActionResult Create(Project project)
        {
            if (ModelState.IsValid)
            {
                project.RecordTime = DateTime.Now;
                project.UserID = User.Identity.Name;
                db.Entry(project).State = EntityState.Added;
                //db.Projects.Add(project);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(project);
        }
        
        //
        // GET: /Project/Edit/5
 
        public ActionResult Edit(string id)
        {
            Project project = db.Projects.Find(id);
            return View(project);
        }

        //
        // POST: /Project/Edit/5

        [HttpPost]
        public ActionResult Edit(Project project)
        {
            if (ModelState.IsValid)
            {
                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(project);
        }

        //
        // GET: /Project/Delete/5
 
        public ActionResult Delete(string id)
        {
            Project project = db.Projects.Find(id);
            return View(project);
        }

        //
        // POST: /Project/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            Project project = db.Projects.Find(id);
            if (project.UserID == User.Identity.Name)
            {
                db.Projects.Remove(project);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                //不是发布人无法删除
                return View(project);
            }
        }

        public ActionResult SetClose(string id)
        {    
            Project project = db.Projects.Find(id);
            return View(project);
        }

        //
        // POST: /Project/Edit/5

        [HttpPost, ActionName("SetClose")]
        public ActionResult SetCloseConfirmed(string id)
        {
            Project project = db.Projects.Find(id);
            try
            {
                project.ProjectStatus = "CLOSE";
                db.Entry(project).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(project);
            }
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}